//************************************************************
//
// DATASTEP 6.  CHOOSING BETA AND SIGMA AMONG THE MANY PRODUCED
//
// AUTHOR: LIONEL NESTA
//
// DATASOURCE: USING final_reaction_functions.dta FROM DATASTEP 5
//
//*************************************************************


// PREAMBLE
*set processors 24

clear
clear matrix 
clear mata
set more off
set mem 600m
set matsize 800

cd "TO BE ADAPTED"

// OPENING FILE

use final_reaction_functions_inter.dta, clear

* HERE WE DECIDE WHICH SIGMA TO USE

rename sigma_2_gr_corr c_market_c

rename beta c_techno_c

rename sigma_2 c_market_c2

// DROPPING EXTREME VALUES OF SIGMA ADN BETA (-1 and 1)

recast float c_market_c, force
replace c_market_c = . if c_market_c == -1
replace c_market_c = . if c_market_c == 1

recast float c_techno_c, force
replace c_techno_c = . if c_techno_c == -1
replace c_techno_c = . if c_techno_c == 1

// BUILDING YEAR DUMMIES

tabulate year, gen(year_)

// LAGGING

gen L1lnrd1 = l.lnrd1 
gen L1lnrdi1 = l.lnrdi1 
gen L1LI1 = l.LI1
gen L1lnk1 = l.lnk1 
gen L1lnliq_k1 = l.lnliq_k1
gen L1lnliq_ratio1 = l.lnliq_ratio1
gen L1msh1 = l.msh1  
gen L1lnrd2 = l.lnrd2
gen L1lnrdi2 = l.lnrdi2 
gen L2lnrd2 = l2.lnrd2 
gen L1lnrdpatsm1 = l1.lnrdpatsm1
gen L2lnrdpatsm1 = l2.lnrdpatsm1 
gen L1lnrdpatsm2 = l1.lnrdpatsm2
gen L2lnrdpatsm2 = l2.lnrdpatsm2 

gen L1LI2 = l.LI2
gen L1lnk2 = l.lnk2 
gen L1lnliq_k2 = l.lnliq_k2
gen L1lnliq_ratio2 = l.lnliq_ratio2


egen temp = mean(L2lnrdpatsm1), by(gvkey1)
xtile pct_eff_rd = temp, nq(5)
drop temp

gen rd_eff_sym = L1lnrdpatsm1 - L2lnrdpatsm2
summarize rd_eff_sym
gen sym_indi =  abs(rd_eff_sym) < r(sd)
egen temp =  median(sym_indi), by(gvkey1 gvkey2)
replace sym_indi = temp
drop temp 


gen lnk12 = lnk1 * lnk1
gen lnk22 = lnk2 * lnk2


* ASSUMING HIGH GAMMA
* GROWTH RATE

gen     case1 = 1 if c_market_c > 0 & c_techno_c > 0
replace case1 = 2 if c_market_c < 0 & c_techno_c > 0
replace case1 = 3 if c_market_c < 0 & c_techno_c < 0
replace case1 = 4 if c_market_c > 0 & c_techno_c < 0

replace case1 = 4 if case1 == 1 & c_market_c > 2 * c_techno_c 
replace case1 = 2 if case1 == 3 & c_market_c < 2 * c_techno_c 
replace case1 = . if c_market_c == .
replace case1 = . if c_techno_c == .


* LEVELS
gen     case2 = 1 if c_market_c2 > 0 & c_techno_c > 0
replace case2 = 2 if c_market_c2 < 0 & c_techno_c > 0
replace case2 = 3 if c_market_c2 < 0 & c_techno_c < 0
replace case2 = 4 if c_market_c2 > 0 & c_techno_c < 0

replace case2 = 4 if case2 == 1 & c_market_c2 > 2 * c_techno_c 
replace case2 = 2 if case2 == 3 & c_market_c2 < 2 * c_techno_c 
replace case2 = . if c_market_c2 == .
replace case2 = . if c_techno_c == .


* ASSUMING LOW GAMMA
* GROWTH RATE
gen     case3 = case1
replace case3 = 2 if case3 == 1 & c_market_c < .3
replace case3 = 3 if case3 == 4 & c_market_c < .5
replace case3 = . if c_market_c == .
replace case3 = . if c_techno_c == .

* LEVELS
gen     case4 = case2
replace case4 = 2 if case4 == 1 & c_market_c < .3
replace case4 = 3 if case4 == 4 & c_market_c < .5
replace case4 = . if c_market_c2 == .
replace case4 = . if c_techno_c == .


//  COMPUTING COUPLE ID CID

tostring gvkey1, gen(f1)
tostring gvkey2, gen(f2)
gen f12 = f1 + "_" + f2 if gvkey1 < gvkey2 
replace f12 = f2 + "_" + f1 if f12 == ""
egen did = group(f12)
order did f12
sort did


egen md_case1 = median(case1), by(did)

egen md_case2 = median(case2), by(did)

* Building instruments

egen mrd_sec1 = sum(rd1), by(sector1 year)
egen mrd_sec2 = sum(rd2), by(sector2 year)

egen n1 = count(rd1), by(sector1 year)
egen n2 = count(rd2), by(sector2 year)

gen mrd_sec1_fs = ln((mrd_sec1 - rd1)/(n1 -1))
gen mrd_sec2_fs = ln((mrd_sec2 - rd2)/(n2 -1))

// SAVING

save final_reaction_functions.dta, replace
